<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/FCKeditor.tld" prefix="fck" %>
<%@page import="java.util.*,model.*,util.*,control.*,java.text.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>开始测试</title>
<script src="../../SpryAssets/SpryValidationSelect.js" type="text/javascript"></script>
<link href="../../SpryAssets/SpryValidationSelect.css" rel="stylesheet" type="text/css">
</head>
<body>
<%
	request.setCharacterEncoding("utf-8");
	Problem problem = new Problem();
	ProblemControl pc = new ProblemControl();
	
	
	//通过内建对象request获取传过来的数据
	int id = ParamUtil.getInt(request,"id",0);
	int n = ParamUtil.getInt(request,"n",0);
	double cita= ParamUtil.getDouble(request,"cita",0.0);
	double ffirst = ParamUtil.getDouble(request,"ffirst",0.0);
	double fsecond = ParamUtil.getDouble(request,"fsecond",0.0);
	String startDate = ParamUtil.getString(request,"startDate");
	
	//通过单选表单获取考生选择的答案
	String choose = request.getParameter("choose");
	//System.out.println("choose:" + choose);
	
	//获得刚刚那道题目,以便获得ab值
	problem = pc.GetProblem(id);
	//System.out.println("preTime: "+ preDate);
	
	/*
	获得上一个时间、 更改每道题的使用次数、获取系统时间
	by vinllen 
	2011-11-27
	*/
	String preDate = ParamUtil.getString(request, "nowDate");

	problem.setSelectNum(problem.getSelectNum() + 1);
	
	Calendar ca = Calendar.getInstance();  
    int year = ca.get(Calendar.YEAR);//获取年份  
    int month=ca.get(Calendar.MONTH) + 1;//获取月份   
    int day=ca.get(Calendar.DATE);//获取日  
    int minute=ca.get(Calendar.MINUTE);//分   
    int hour=ca.get(Calendar.HOUR);//小时
    int second=ca.get(Calendar.SECOND);//秒  
    String nowDate = year + "-" + month+ "-" + day + "-"+ hour + ":" + minute + ":" + second;
	
	DateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss"); 
	Date d1 = df.parse(preDate);
	Date d2 = df.parse(nowDate);
	long diff = (d2.getTime() - d1.getTime()) / 1000; //以防万一设置long 除以1000转换成秒
	
	
	String tmpString = Long.toString(diff);
	problem.setResponseTime(tmpString);
	
	
	
	/*
	*对能力进行估计
	*/
	int u=0;//u是状态 刚回答正确了u=1 否则u=0
	if(problem.getCorrect().equals(choose)) {//如果回答正确
		u = 1;
		//更改每道题的答对次数
		problem.setCorrectNum(problem.getCorrectNum() + 1);
	}
	else 
		u = 0;
		
	/******************************************************************************/
	/*
	*查所有人的成绩
	*update 胡瑾
	*data 2011-11-27
	*update hujin
	*data 2011-12-06 加入testTime
	*/
	String userId = (String)session.getAttribute("userId");
	int studentId = Integer.parseInt(userId);
	Answerlog answerlog = new Answerlog();
	AnswerlogControl alc = new AnswerlogControl();
	
	answerlog.setStudentId(studentId);
	answerlog.setId(id);
	//等星哥的反应时间
	answerlog.setAwTime(tmpString);
	
	//1就是做对了 0就是做错了
	if(u==1)answerlog.setFlag("1");
	else answerlog.setFlag("0");
	answerlog.setTestTime(startDate);
	
	alc.insertAnswerlog(answerlog);

	
	/******************************************************************************/	
		
	
	
	double a=problem.getAa();//获得a
	double b=problem.getBb();//获得b
	String Chapter = problem.getChapter();
	String Section = problem.getSection();
	double p=pc.GetP(a,b,cita);//获得P
	double q=1-p;
	double D=1.702;//是常数
	double c = 1/4;
	ffirst = ffirst+D*a*(u-p)*(p-c); //每做一题叠加一次 即求和 
	double ffirstnew = ffirst/(p*(1-c));
	fsecond = fsecond+(D*D)*a*a*(u*c-p*p)*(p-c)*q;//同上一行
	double fsecondnew = fsecond/(p*p*(1-c)*(1-c));
	
	double ht=ffirstnew/fsecondnew;//得到那个差值
	cita = cita - ht;//新的cita = 旧的cita - ht
	//System.out.println(cita);
	
	/*
	update一下下
	by vinllen 
	2011-11-27
	*/
	pc.updateProblem_tongji(problem, id);
	//System.out.println("分别结果：a:"+a+" b:"+b+" p:"+p+" ffirst:"+ffirst+"zhongjian"+D*a*(u-p)+" cita:"+cita);
	
	if(u==1){
	
		if((n>5&&Math.abs(ht)<0.1)||n>10)//终止条件 要求总体数要大于5  并且相邻两个cita要小于0.01  才能终止
		//不能终止就传参到problem_next 获得下一道题
		{
%>
<script language="javascript">
     alert("恭喜您！回答正确!您的测试已结束");
     window.location.href="problem_finish.jsp?n=<%=n%>&cita=<%=cita%>&startDate=<%=startDate%>&tmpString=<%=tmpString%>&nowDate=<%=nowDate%>";
    </script>
<%
		}//if2
		else{
%>
<script language="javascript">
	     alert("恭喜您！回答正确!");
	     window.location.href="problem_next.jsp?n=<%=n+1%>&cita=<%=cita%>&ffirst=<%=ffirst%>&fsecond=<%=fsecond%>&startDate=<%=startDate%>";
	    </script>
<% 
		}//else2
	}//if1
	
	
	else{
		if((n>5&&Math.abs(ht)<0.1)||n>10){//终止条件
%>
<script language="javascript">
	     alert("回答错误 您的测试已结束\n该知识点在第"+<%=Chapter%>+"章 第"+<%=Section%>+"节");
	     window.location.href="problem_finish.jsp?n=<%=n%>&cita=<%=cita%>&startDate=<%=startDate%>&tmpString=<%=tmpString%>&nowDate=<%=nowDate%>";
	    </script>
<%
		}//if2
		else{
		//System.out.println("shshhs");
%>
<script language="javascript">
	     alert("回答错误\n该知识点在第"+<%=Chapter%>+"章 第"+<%=Section%>+"节");
	     //alert("回答错误");
	     window.location.href="problem_next.jsp?n=<%=n+1%>&cita=<%=cita%>&ffirst=<%=ffirst%>&fsecond=<%=fsecond%>&startDate=<%=startDate%>";
	    </script>
<%
  		}
  	}	
  %>
</html>
